<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>

</head>
<jsp:include page="../default.jsp" />
<body>
	<div class="mini-splitter" style="width: 100%; height: 100%;">
		<div size="240" showCollapseButton="true">
			<div class="mini-toolbar"
				style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
				<span style="padding-left: 5px;">名称：</span> <input
					class="mini-textbox" name="name" /> <a class="mini-button"
					iconCls="icon-search" onclick="query()">查询</a>
			</div>
			<div class="mini-fit">
				<ul id="tree" class="mini-tree"
					url="${pageContext.request.contextPath}/auth/menu/query.do"
					textField="name" idField="id" parentField="parentId"
					expandOnLoad="true" resultAsTree="false"
					onload="PageHelper.prototype.commonError">
				</ul>
			</div>
		</div>
		<div showCollapseButton="true">
			<div class="mini-toolbar"
				style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
				<a class="mini-button" iconCls="icon-add" onclick="toForm('add')">新增</a>
				<a class="mini-button" iconCls="icon-remove" onclick="toRemove()">删除</a>
			</div>
			<div class="mini-fit">
				<div id="form" align="center">
					<input name="id" class="mini-hidden" />
					<table align="center" width="100%" border="0" cellpadding="0"
						cellspacing="0">
						<tr height="30px">
							<td align="right">名称：</td>
							<td><input name="name" class="mini-textbox"
								vtype="maxLength:15;" required="true" /></td>
							<td align="right">父级菜单：</td>
							<td><input name="parentId" class="mini-treeselect"
								url="${pageContext.request.contextPath}/auth/menu/query.do"
								textField="name" valueField="id" parentField="parentId"
								expandOnLoad="true" resultAsTree="false"
								onload="PageHelper.prototype.commonError" /></td>
						</tr>
						<tr height="30px">
							<td align="right">链接：</td>
							<td><input name="url" class="mini-textbox"
								vtype="maxLength:50;" /></td>
							<td align="right">排序：</td>
							<td><input name="sort" class="mini-textbox"
								vtype="int;range:0,999;" /></td>
						</tr>
					</table>
				</div>
			</div>
			<div class="mini-toolbar"
				style="text-align: center; border-bottom: 0; border-left: 0; border-right: 0;">
				<a class="mini-button" iconCls="icon-save" onclick="save()"
					id="saveBtn">保存</a>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		mini.parse();
		var form = new mini.Form("#form");
		var tree = mini.get("tree");
		window.action = 'add';
		tree.on("nodeselect", function(e) {
			toForm('edit', e.node.id);
		});

		function query() {
			var key = mini.getbyName("name").getValue();
			if (key == "") {
				tree.clearFilter();
			} else {
				tree.filter(function(node) {
					var text = node.name ? node.name : "";
					if (text.indexOf(key) != -1) {
						return true;
					}
				});
			}
		}

		function toForm(action, id) {
			if (!id) {
				id = "";
			}
			mini.getbyName("parentId").load(
					"${pageContext.request.contextPath}/auth/menu/query.do?excludeId="
							+ id);
			form.clear();
			window.action = action;
			if (action == 'edit') {
				CommonUtil.prototype.mask();
				$
						.ajax({
							url : "${pageContext.request.contextPath}/auth/menu/load.do",
							data : {
								id : id
							},
							type : "POST",
							dataType : "json",
							success : function(data, textStatus, jqXHR) {
								CommonUtil.prototype.unmask();
								if (data.success) {
									window.action = 'edit';
									form.setData(data.data);
									form.setChanged(false);
								}
							}
						});
			}
			form.setChanged(false);
		}

		function save() {
			form.validate();
			if (form.isValid() == false)
				return;
			CommonUtil.prototype.mask();
			$
					.ajax({
						url : "${pageContext.request.contextPath}/auth/menu/save.do?action="
								+ window.action,
						data : form.getData(true),
						type : "POST",
						dataType : "json",
						success : function(data, textStatus, jqXHR) {
							CommonUtil.prototype.unmask();
							if (data.success) {
								mini.alert("保存成功", "提醒", function() {
									form.clear();
									tree.reload();
									var node = tree.getSelectedNode();
									if (node) {
										toForm('edit', node.id);
									} else {
										toForm('add');
									}
								});
							}
						}
					});
		}

		function toRemove() {
			var node = tree.getSelectedNode();
			if (!node) {
				mini.alert("请选中一条记录");
				return;
			}
			mini
					.confirm(
							"确认删除",
							"提醒",
							function(action) {
								if (action == "ok") {
									CommonUtil.prototype.mask();
									$
											.ajax({
												url : "${pageContext.request.contextPath}/auth/menu/remove.do",
												data : {
													id : node.id
												},
												type : "POST",
												dataType : "json",
												success : function(data,
														textStatus, jqXHR) {
													CommonUtil.prototype
															.unmask();
													if (data.success) {
														mini
																.alert(
																		"删除成功",
																		"提醒",
																		function() {
																			form
																					.clear();
																			tree
																					.reload();
																			var node = tree
																					.getSelectedNode();
																			if (node) {
																				toForm(
																						'edit',
																						node.id);
																			} else {
																				toForm('add');
																			}
																		});
													}
												}
											});
								}
							});
		}
	</script>
</body>
</html>
